﻿using System;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using HslCommunication.Core;
using HslCommunication.Core.Net;
using HslCommunication.Reflection;

namespace HslCommunication.Profinet.Omron
{
	/// <summary>
	/// 欧姆龙的HostLink的C-Mode实现形式，当前的类是通过以太网透传实现。地址支持携带站号信息，例如：s=2;D100<br />
	/// The C-Mode implementation form of Omron’s HostLink, the current class is realized through Ethernet transparent transmission. 
	/// Address supports carrying station number information, for example: s=2;D100
	/// </summary>
	/// <remarks>
	/// 暂时只支持的字数据的读写操作，不支持位的读写操作。
	/// </remarks>
	// Token: 0x02000061 RID: 97
	public class OmronHostLinkCModeOverTcp : NetworkDeviceSoloBase
	{
		/// <inheritdoc cref="M:HslCommunication.Profinet.Omron.OmronFinsNet.#ctor" />
		// Token: 0x060007FA RID: 2042 RVA: 0x0002AFDC File Offset: 0x000291DC
		public OmronHostLinkCModeOverTcp()
		{
			base.ByteTransform = new ReverseWordTransform();
			base.WordLength = 1;
			base.ByteTransform.DataFormat = DataFormat.CDAB;
			base.ByteTransform.IsStringReverseByteWord = true;
		}

		/// <inheritdoc cref="M:HslCommunication.Profinet.Omron.OmronCipNet.#ctor(System.String,System.Int32)" />
		// Token: 0x060007FB RID: 2043 RVA: 0x0002B014 File Offset: 0x00029214
		public OmronHostLinkCModeOverTcp(string ipAddress, int port)
		{
			base.ByteTransform = new ReverseWordTransform();
			base.WordLength = 1;
			base.ByteTransform.DataFormat = DataFormat.CDAB;
			base.ByteTransform.IsStringReverseByteWord = true;
			this.IpAddress = ipAddress;
			this.Port = port;
		}

		/// <inheritdoc cref="P:HslCommunication.Profinet.Omron.OmronHostLinkOverTcp.UnitNumber" />
		// Token: 0x17000171 RID: 369
		// (get) Token: 0x060007FC RID: 2044 RVA: 0x0002B067 File Offset: 0x00029267
		// (set) Token: 0x060007FD RID: 2045 RVA: 0x0002B06F File Offset: 0x0002926F
		public byte UnitNumber { get; set; }

		/// <inheritdoc cref="M:HslCommunication.Profinet.Omron.OmronFinsNet.Read(System.String,System.UInt16)" />
		// Token: 0x060007FE RID: 2046 RVA: 0x0002B078 File Offset: 0x00029278
		[HslMqttApi("ReadByteArray", "")]
		public override OperateResult<byte[]> Read(string address, ushort length)
		{
			byte unitNumber = (byte)HslHelper.ExtractParameter(ref address, "s", (int)this.UnitNumber);
			OperateResult<byte[]> operateResult = OmronHostLinkCMode.BuildReadCommand(address, length, false);
			bool flag = !operateResult.IsSuccess;
			OperateResult<byte[]> result;
			if (flag)
			{
				result = operateResult;
			}
			else
			{
				OperateResult<byte[]> operateResult2 = base.ReadFromCoreServer(OmronHostLinkCMode.PackCommand(operateResult.Content, unitNumber));
				bool flag2 = !operateResult2.IsSuccess;
				if (flag2)
				{
					result = OperateResult.CreateFailedResult<byte[]>(operateResult2);
				}
				else
				{
					OperateResult<byte[]> operateResult3 = OmronHostLinkCMode.ResponseValidAnalysis(operateResult2.Content, true);
					bool flag3 = !operateResult3.IsSuccess;
					if (flag3)
					{
						result = OperateResult.CreateFailedResult<byte[]>(operateResult3);
					}
					else
					{
						result = OperateResult.CreateSuccessResult<byte[]>(operateResult3.Content);
					}
				}
			}
			return result;
		}

		/// <inheritdoc cref="M:HslCommunication.Profinet.Omron.OmronFinsNet.Write(System.String,System.Byte[])" />
		// Token: 0x060007FF RID: 2047 RVA: 0x0002B11C File Offset: 0x0002931C
		[HslMqttApi("WriteByteArray", "")]
		public override OperateResult Write(string address, byte[] value)
		{
			byte unitNumber = (byte)HslHelper.ExtractParameter(ref address, "s", (int)this.UnitNumber);
			OperateResult<byte[]> operateResult = OmronHostLinkCMode.BuildWriteWordCommand(address, value);
			bool flag = !operateResult.IsSuccess;
			OperateResult result;
			if (flag)
			{
				result = operateResult;
			}
			else
			{
				OperateResult<byte[]> operateResult2 = base.ReadFromCoreServer(OmronHostLinkCMode.PackCommand(operateResult.Content, unitNumber));
				bool flag2 = !operateResult2.IsSuccess;
				if (flag2)
				{
					result = operateResult2;
				}
				else
				{
					OperateResult<byte[]> operateResult3 = OmronHostLinkCMode.ResponseValidAnalysis(operateResult2.Content, false);
					bool flag3 = !operateResult3.IsSuccess;
					if (flag3)
					{
						result = operateResult3;
					}
					else
					{
						result = OperateResult.CreateSuccessResult();
					}
				}
			}
			return result;
		}

		/// <inheritdoc cref="M:HslCommunication.Profinet.Omron.OmronHostLinkCMode.Read(System.String,System.UInt16)" />
		// Token: 0x06000800 RID: 2048 RVA: 0x0002B1B0 File Offset: 0x000293B0
		[DebuggerStepThrough]
		public override Task<OperateResult<byte[]>> ReadAsync(string address, ushort length)
		{
			OmronHostLinkCModeOverTcp.<ReadAsync>d__8 <ReadAsync>d__ = new OmronHostLinkCModeOverTcp.<ReadAsync>d__8();
			<ReadAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult<byte[]>>.Create();
			<ReadAsync>d__.<>4__this = this;
			<ReadAsync>d__.address = address;
			<ReadAsync>d__.length = length;
			<ReadAsync>d__.<>1__state = -1;
			<ReadAsync>d__.<>t__builder.Start<OmronHostLinkCModeOverTcp.<ReadAsync>d__8>(ref <ReadAsync>d__);
			return <ReadAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc cref="M:HslCommunication.Profinet.Omron.OmronHostLinkCMode.Write(System.String,System.Byte[])" />
		// Token: 0x06000801 RID: 2049 RVA: 0x0002B204 File Offset: 0x00029404
		[DebuggerStepThrough]
		public override Task<OperateResult> WriteAsync(string address, byte[] value)
		{
			OmronHostLinkCModeOverTcp.<WriteAsync>d__9 <WriteAsync>d__ = new OmronHostLinkCModeOverTcp.<WriteAsync>d__9();
			<WriteAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
			<WriteAsync>d__.<>4__this = this;
			<WriteAsync>d__.address = address;
			<WriteAsync>d__.value = value;
			<WriteAsync>d__.<>1__state = -1;
			<WriteAsync>d__.<>t__builder.Start<OmronHostLinkCModeOverTcp.<WriteAsync>d__9>(ref <WriteAsync>d__);
			return <WriteAsync>d__.<>t__builder.Task;
		}

		/// <summary>
		/// 读取PLC的当前的型号信息
		/// </summary>
		/// <returns>型号</returns>
		// Token: 0x06000802 RID: 2050 RVA: 0x0002B258 File Offset: 0x00029458
		[HslMqttApi]
		public OperateResult<string> ReadPlcModel()
		{
			OperateResult<byte[]> operateResult = base.ReadFromCoreServer(OmronHostLinkCMode.PackCommand(Encoding.ASCII.GetBytes("MM"), this.UnitNumber));
			bool flag = !operateResult.IsSuccess;
			OperateResult<string> result;
			if (flag)
			{
				result = OperateResult.CreateFailedResult<string>(operateResult);
			}
			else
			{
				int num = Convert.ToInt32(Encoding.ASCII.GetString(operateResult.Content, 5, 2), 16);
				bool flag2 = num > 0;
				if (flag2)
				{
					result = new OperateResult<string>(num, "Unknown Error");
				}
				else
				{
					string @string = Encoding.ASCII.GetString(operateResult.Content, 7, 2);
					result = OmronHostLinkCMode.GetModelText(@string);
				}
			}
			return result;
		}

		/// <inheritdoc />
		// Token: 0x06000803 RID: 2051 RVA: 0x0002B2F0 File Offset: 0x000294F0
		public override string ToString()
		{
			return string.Format("OmronHostLinkCModeOverTcp[{0}:{1}]", this.IpAddress, this.Port);
		}
	}
}
